<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('发生日期')" prop="operateDate">
            <DatePicker v-model="form.operateDate" :placeholder="$t('发生日期')" />
          </el-form-item>
        </el-col>
        <!--        <el-col :span="12">-->
        <!--          <el-form-item :label="$t('公司')" prop="companyCosts">-->
        <!--            <Money v-model="form.companyCosts" :placeholder="$t('公司')" />-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <!--        <el-col :span="12">-->
        <!--          <el-form-item :label="$t('产品')" prop="chanPinCosts">-->
        <!--            <Money v-model="form.chanPinCosts" :placeholder="$t('产品')" />-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <!--        <el-col :span="12">-->
        <!--          <el-form-item :label="$t('何平')" prop="hePinCosts">-->
        <!--            <Money v-model="form.hePingCosts" :placeholder="$t('何平')" />-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <!--        <el-col :span="12">-->
        <!--          <el-form-item :label="$t('路爽')" prop="luShuangCosts">-->
        <!--            <Money v-model="form.luShuangCosts" :placeholder="$t('路爽')" />-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <!--        <el-col :span="12">-->
        <!--          <el-form-item :label="$t('刘高俊')" prop="liuGaoJunCosts">-->
        <!--            <Money v-model="form.liuGaoJunCosts" :placeholder="$t('刘高俊')" />-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <!--        <el-col :span="12">-->
        <!--          <el-form-item :label="$t('耿益辉')" prop="gengYiHuiCosts">-->
        <!--            <Money v-model="form.gengYiHuiCosts" :placeholder="$t('耿益辉')" />-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <!--        <el-col :span="24">-->
        <!--          <el-form-item :label="$t('备注')" prop="remark">-->
        <!--            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" :maxlength="2000" show-word-limit />-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
      </el-row>
      <div class="area-header" style="margin-top: 10px">
        <span class="area-header-title">{{ $t('明细') }}</span>
      </div>
      <el-table tooltip-effect="darkTable" :data="form.companyCostsDetailList" border>
        <el-table-column :label="$t('部门')" prop="companyDept" align="center">
          <template slot-scope="scope">
            <el-form-item :prop="'companyCostsDetailList.' + scope.$index + '.companyDept'">
              <tree-select v-model="scope.row.companyDept" node-key="id" :data="deptList" :props="propsOptions" :placeholder="$t('请选择部门')">
                <label
                  slot="option-label"
                  slot-scope="{ node, shouldShowCount, count, labelClassName, countClassName }"
                  :class="labelClassName"
                >
                  {{ $t(node.label) }}
                  <span v-if="shouldShowCount" :class="countClassName">({{ count }})</span>
                </label>
              </tree-select>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('税金')" prop="taxes" align="center">
          <template slot-scope="scope">
            <el-form-item :prop="'companyCostsDetailList.' + scope.$index + '.taxes'">
              <money v-model="scope.row.taxes" :placeholder="$t('税金')" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('人力')" prop="manpower" align="center">
          <template slot-scope="scope">
            <el-form-item :prop="'companyCostsDetailList.' + scope.$index + '.manpower'">
              <money v-model="scope.row.manpower" :placeholder="$t('人力')" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('其他')" prop="other" align="center">
          <template slot-scope="scope">
            <el-form-item :prop="'companyCostsDetailList.' + scope.$index + '.other'">
              <money v-model="scope.row.other" :placeholder="$t('其他')" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('操作')" prop="" align="center" width="80">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-delete" @click="delCompanyCostsDetail(scope.$index)">{{ $t('删除') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="tol">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addCompanyCostsDetail">{{ $t("添加") }}</el-button>
      </div>
      <!-- 附件列表 -->
      <FileTable :list.sync="form.fileList" data-type="anxin_company_costs" :btn-show="!otherBtnHide" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <save-btn has-permi="anxin:costs" />
    </div>
  </div>
</template>

<script>
import {
  saveAnxinCompanyCosts,
  getAnxinCompanyCosts
} from '@/api/anxin/costs/anxinCompanyCosts'
import { findDeptByInvestmentTeam } from '@/api/system/dept'
import TreeSelect from '@/components/TreeSelect'
export default {
  components: {
    TreeSelect
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      propsOptions: {
        children: 'children',
        label: 'label'
      },
      // 是否隐藏其它按钮
      otherBtnHide: false,
      deptList: [],
      // 表单校验
      rules: {
        operateDate: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ],
        companyCosts: [
          { required: true, message: this.$t('必填信息'), trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    findDeptByInvestmentTeam({ type: 'tree-company' }).then(res => {
      this.deptList = res.data
    })
  },
  methods: {
    // 初始化数据
    init(data) {
      if (data != null && data.id !== undefined) {
        this.loading = true
        getAnxinCompanyCosts(data.id).then((response) => {
          this.form = response.data
          this.loading = false
        })
      } else {
        this.reset()
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        operateDate: undefined,
        companyCosts: 0,
        chanPinCosts: 0,
        hePingCosts: 0,
        luShuangCosts: 0,
        liuGaoJunCosts: 0,
        gengYiHuiCosts: 0,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        companyCostsDetailList: [],
        fileList: []
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
      this.$parent.$parent.reset()
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      this.$parent.$parent.formOpen = false
      // 刷新list页面
      this.$parent.$parent.getList()
    },
    /** 提交按钮 */
    submitForm: function() {
      if (!this.form.companyCostsDetailList || this.form.companyCostsDetailList.length === 0) {
        this.$message.error(this.$t('请维护明细后提交'))
        return
      }
      var isCompany = false
      this.form.companyCostsDetailList.forEach(item => {
        if (item.companyDept === 'COMPANY') {
          isCompany = true
        }
      })
      if (!isCompany) {
        this.$message.error(this.$t('公司费用必须维护'))
        return
      }
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          saveAnxinCompanyCosts(this.form)
            .then((response) => {
              if (response.code === 200) {
                this.msgSuccess(this.$t('保存成功'))
                this.closeForm()
              } else {
                this.msgError(response.msg)
              }
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          this.locationError(document)
        }
      })
    },
    // 添加明细
    addCompanyCostsDetail() {
      const row = {
        id: undefined,
        companyDept: undefined,
        taxes: undefined,
        manpower: undefined,
        other: undefined
      }
      if (!(this.form.companyCostsDetailList instanceof Array)) {
        this.$set(this.form, 'companyCostsDetailList', [])
      }
      this.$nextTick(() => {
        this.form.companyCostsDetailList.push(row)
      })
    },
    // 删除明细
    delCompanyCostsDetail(index) {
      this.form.companyCostsDetailList.splice(index, 1)
    }
  }
}
</script>
